package com.haoyi.feignapi.controller;

import com.github.pagehelper.PageInfo;
import com.haoyi.feignapi.entity.erp.ErpImg;
import com.haoyi.feignapi.entity.erp.ErpOCC;
import com.haoyi.feignapi.service.ErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/erp")
public class ErpController {

    @Autowired
    ErpService erpService;

    @GetMapping("/feignTest")
    public String feignTest() {
        return erpService.feignTest();
    }

    @PostMapping("/getErpCode")
    public List<String> getErpCode(@RequestBody List<String> search) {
        return erpService.getErpCode(search);
    }

    @PostMapping("/getErpInventory")
    public PageInfo<Map<String, Object>> getErpPartsgetErpInventory(@RequestBody Map<String, Object> paramMap) {
        return erpService.getErpInventory(paramMap);
    }

    @PostMapping("/getErpParts")
    public PageInfo<Map<String, Object>> getErpParts(@RequestBody Map<String, Object> paramMap) {
        return erpService.getErpParts(paramMap);
    }


    @PostMapping("/getOrderErpCode")
    List<Map<String, Object>> getOrderErpCode(@RequestBody Map<String, Object> paramMap) {
        return erpService.getOrderErpCode(paramMap);
    }

    @GetMapping("/getCode")
    public ErpOCC getCode(@RequestParam("company") String company) {
        return erpService.getCode(company);
    }

    @GetMapping("/allMaterial")
    public ArrayList<ErpImg> allMaterial() {
        ArrayList<ErpImg> imgs = erpService.allMaterial();
        return imgs;
    }

    @PostMapping("/different")
    public ErpImg different(@RequestParam String item ,@RequestParam String item1) {
//        ArrayList<String> item = new ArrayList<>();
//        ArrayList<String> item1 = new ArrayList<>();
//        ArrayList<ErpImg> allDifferent = new ArrayList<>();
//        for (ErpImg erpImg:different){
//            String getImg01 = erpImg.getImg01();
//            item.add(getImg01);
//            String getImg02 =erpImg.getImg02();
//            item1.add(getImg02);
           return erpService.different(item,item1);
        }

//        List<List<String>> itemBatches = partition(item, 1000);
//        List<List<String>> item1Batches = partition(item1, 1000);
//
//        ArrayList<ErpImg> allDifferent = new ArrayList<>();
//
//        for (int i = 0; i < itemBatches.size(); i++) {
//            ArrayList<String> itemBatch = new ArrayList<>(itemBatches.get(i));
//            ArrayList<String> item1Batch = new ArrayList<>(item1Batches.get(i));
//
//            List<ErpImg> batchResult = erpService.different(itemBatch, item1Batch);
//            allDifferent.addAll(batchResult);
//        }
//        return allDifferent;


    public <T> List<List<T>> partition(List<T> list, int size) {
        List<List<T>> partitions = new ArrayList<>();
        for (int i = 0; i < list.size(); i += size) {
            partitions.add(list.subList(i, Math.min(i + size, list.size())));
        }
        return partitions;
    }

}
